﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace music_control
{
    public class KeyObserver : INotifyPropertyChanged
    {
        private int _startstop;
        public int startstop
        {
            get
            {
                if (_startstop == 0)
                    this._startstop = Properties.Settings.Default.startstop;

                return this._startstop;
            }
            set
            {
                Properties.Settings.Default.startstop = value;
                this._startstop = value;
                ViewModel.isChanged = true;
                RaisePropertyChanged("startstop");
            }
        }

        private int _next;
        public int next
        {
            get
            {
                if (_next == 0)
                    this._next = Properties.Settings.Default.next;

                return this._next;
            }

            set
            {
                Properties.Settings.Default.next = value;
                this._next = value;
                ViewModel.isChanged = true;
                RaisePropertyChanged("next");
            }
        }

        private int _previous;
        public int previous
        {
            get
            {
                if (_previous == 0)
                    this._previous = Properties.Settings.Default.previous;

                return this._previous;
            }

            set
            {
                Properties.Settings.Default.previous = value;
                this._previous = value;
                ViewModel.isChanged = true;
                RaisePropertyChanged("previous");
            }
        }


        private int _lastpressed;
        public int lastpressed
        {
            get
            {
                return this. _lastpressed;
            }

            set
            {
                this. _lastpressed = value;
                RaisePropertyChanged("lastpressed");
            }
        }

        public delegate void KeyPressedEventHandler(object sender, PlayerKeyPressedEventArgs e);

        public KeyObserver()
        {
            Hook.CreateHook(KeyReader);
        }

        public event KeyPressedEventHandler KeyPressed;

        public void KeyReader(IntPtr wParam, IntPtr lParam)
        {
            int key = Marshal.ReadInt32(lParam);

            lastpressed = key;

            if (startstop == key)
            {
                KeyPressed(this, new PlayerKeyPressedEventArgs(PlayerControlEnum.StartStop));
            }

            if (next == key)
            {
                KeyPressed(this, new PlayerKeyPressedEventArgs(PlayerControlEnum.NextTrack));
            }

            if (previous == key)
            {
                KeyPressed(this, new PlayerKeyPressedEventArgs(PlayerControlEnum.PreviouseTrack));
            }
        }

        public void ResetSettings()
        {
            startstop = Properties.Settings.Default.startstop = Properties.Settings.Default.startstop_default;
            previous = Properties.Settings.Default.previous = Properties.Settings.Default.previous_default;
            next = Properties.Settings.Default.next = Properties.Settings.Default.next_default;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }    

    }

    public class PlayerKeyPressedEventArgs : EventArgs
    {
        public PlayerControlEnum PressedKey;

        public PlayerKeyPressedEventArgs(PlayerControlEnum pressed)
        {
            PressedKey = pressed;
        }
    }

    

}
